<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改车辆信息')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-manage-edit" th:object="${tmdCarList}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">编号：</label>
                <div class="col-sm-8">
                    <input name="id" th:field="*{id}"class="form-control" type="text" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">车辆名称：</label>
                <div class="col-sm-8">
                    <input name="carName" th:field="*{carName}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">颜色：</label>
                <div class="col-sm-8">
                    <select name="carColor" class="form-control m-b" th:with="type=${@dict.getType('car_color')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{carColor}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">车牌号：</label>
                <div class="col-sm-8">
                    <input name="carCard" th:field="*{carCard}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">司机：</label>
                <div class="col-sm-8">
                    <select id="driverSelect" name="driverId" class="form-control m-b">
                        <option value="">请选择司机</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">手机号：</label>
                <div class="col-sm-8">
                    <input id="phoneNumberInput" type="text" name="phonenumber" class="form-control" placeholder="手机号码" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">车辆状态：</label>
                <div class="col-sm-8">
                    <select name="carStatus" class="form-control m-b" th:with="type=${@dict.getType('category_status')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{carStatus}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">附件：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="file" th:field="*{file}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="file" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea name="descirbe" class="form-control">[[*{descirbe}]]</textarea>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "car/manage";
        $("#form-manage-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-manage-edit').serialize());
            }
        }

        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload',
                initialPreviewAsData: true,
                initialPreview: [val],
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
            $(this).fileinput('_initFileActions');
        });

        $(document).ready(function () {
            $.ajax({
                url: prefix + '/getUserList',
                type: 'GET',
                dataType: 'json',
                success: function (response) {
                    // 获取到用户列表后，填充到下拉框中
                    var userList = response.data;
                    var selectElement = $('#driverSelect');
                    $.each(userList, function (index, user) {
                        selectElement.append($('<option>', {
                            value: user.userId,
                            text: user.userName
                        }));
                    });
                },
                error: function (xhr, status, error) {
                    console.error('错误', error);
                }
            });
        });


        // $(document).ready(function () {
        //     $.ajax({
        //         url: prefix + '/getUserList',
        //         type: 'GET',
        //         dataType: 'json',
        //         success: function (response) {
        //             // 获取到用户列表后，填充到下拉框中
        //             var userList = response.data;
        //             var selectElement = $('#driverSelect');
        //             $.each(userList, function (index, user) {
        //                 selectElement.append($('<option>', {
        //                     value: user.userId,
        //                     text: user.userName
        //                 }));
        //             });
        //         },
        //         error: function (xhr, status, error) {
        //             console.error('错误', error);
        //         }
        //     });
        // });

        // 监听下拉框的选择事件
        $('#driverSelect').change(function () {
            var selectedUserId = $(this).val();
            $.ajax({
                url: prefix + '/getUserPhoneNumber',
                type: 'GET',
                dataType: 'json',
                data: {
                    userId: selectedUserId
                },
                success: function (response) {
                    if (response.code === 0) {
                        var phoneNumber = response.data.phonenumber;
                        $('#phoneNumberInput').val(phoneNumber);
                    } else {
                        console.error('获取用户信息失败');
                    }
                },
                error: function (xhr, status, error) {
                    console.error('错误', error);
                }
            });
        });
    </script>
</body>
</html>